package com.central.apps.handler.project;

import com.central.app.common.base.BaseHandler;
import com.central.app.service.HandlerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 处理项目风险业务逻辑
 *
 * @author lidachao
 * @date 2023/5/19
 */
@Component
@Slf4j
public class ProjectWarnHandler extends BaseHandler {

    @Autowired
    private HandlerService handlerService;

    @Override
    public String validateSave(String app, String objName, Map<String, Object> data) throws Exception {
        String warnCategory = MapUtils.getString(data, "warncategory");
        String warnType = MapUtils.getString(data, "warntype");

        Integer id = MapUtils.getInteger(data, "id");
        StringBuilder sql = new StringBuilder();
        sql.append("select id from gq_pbwarncfg where warncategory = '").append(warnCategory).append("' and warntype = '").append(warnType).append("'");

        if (id != null) {
            sql.append(" and id != ").append(id);
        }

        Map<String, Object> map = handlerService.findOneBySql(sql.toString());

        if (map != null && !map.isEmpty()) {
            return "预警规则重复, 请仔细检查并重新维护!";
        }

        return null;
    }
}
